package org.yzh.scheduler.component.annotations;

import org.quartz.CronExpression;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = Cron.CronValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Cron {

    String message() default "格式不正确";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class CronValidator implements ConstraintValidator<Cron, String> {

        @Override
        public void initialize(Cron paramA) {
        }

        @Override
        public boolean isValid(String cronExpression, ConstraintValidatorContext ctx) {
            if (cronExpression == null || cronExpression.isEmpty())
                return false;
            return CronExpression.isValidExpression(cronExpression);
        }
    }
}